﻿namespace SAD.Common.Helper
{
    using System;
    using System.IO;
    using System.Xml.Serialization;

    /// <summary>
    /// 关于XML的序列化辅助方法(泛型只是)
    /// </summary>
    /// <typeparam name="objectType"></typeparam>
    public class SerializationHelper<objectType>
    {
        /// <summary>
        /// 该类不允许被实例化
        /// </summary>
        private SerializationHelper() { }

        #region XmlToObject
        /// <summary>
        /// 将一段序列化的XML还原为某个对象
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static objectType XmlToObject(string strXml)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (StreamWriter sw = new StreamWriter(ms))
                {
                    sw.Write(strXml);
                    sw.Flush();

                    ms.Seek(0, SeekOrigin.Begin);
                    XmlSerializer sr = new XmlSerializer(typeof(objectType));
                    return (objectType)sr.Deserialize(ms);
                }
            }
        }
        #endregion

        #region XmlFileToObject
        /// <summary>
        /// 将一个存储着序列化XML的文件还原为某个对象
        /// </summary>
        /// <param name="strFilePath"></param>
        /// <returns></returns>
        public static objectType XmlFileToObject(string strFilePath)
        {
            using (FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read))
            {
                XmlSerializer sr = new XmlSerializer(typeof(objectType));
                return (objectType)sr.Deserialize(fs);
            }
        }
        #endregion
    }

    /// <summary>
    /// 关于XML的序列化辅助方法
    /// </summary>
    public class Serialization
    {
        #region XmlToObject
        /// <summary>
        /// 将一段序列化的XML还原为某个对象
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static object XmlToObject(string strXml, Type objectType)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (StreamWriter sw = new StreamWriter(ms))
                {
                    sw.Write(strXml);
                    sw.Flush();

                    ms.Seek(0, SeekOrigin.Begin);
                    XmlSerializer sr = new XmlSerializer(objectType);
                    return sr.Deserialize(ms);
                }
            }
        }
        #endregion

        #region XmlFileToObject
        /// <summary>
        /// 将一个存储着序列化XML的文件还原为某个对象
        /// </summary>
        /// <param name="strFilePath"></param>
        /// <returns></returns>
        public static object XmlFileToObject(string strFilePath, Type objectType)
        {
            using (FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read))
            {
                XmlSerializer sr = new XmlSerializer(objectType);
                return sr.Deserialize(fs);
            }
        }
        #endregion

        #region ObjectToXml
        /// <summary>
        /// 将一个对象序列化为一段XML
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ObjectToXml(object obj)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                XmlSerializer sr = new XmlSerializer(obj.GetType());
                sr.Serialize(ms, obj);

                using (StreamReader srr = new StreamReader(ms))
                {
                    ms.Seek(0, SeekOrigin.Begin);
                    return srr.ReadToEnd();
                }
            }
        }
        #endregion

        #region XmlFileToObject
        /// <summary>
        /// 将一个对象序列化为一段XML并保存至指定文件
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="strFilePath"></param>
        public static void ObjectToXmlFile(object obj, string strFilePath)
        {
            File.Delete(strFilePath);
            using (FileStream fs = new FileStream(strFilePath, FileMode.OpenOrCreate))
            {
                XmlSerializer sr = new XmlSerializer(obj.GetType());
                sr.Serialize(fs, obj);
            }
        }
        #endregion
    }
}
